export default{
  // 开启命名空间
  namespaced:true,
  
  // 模块的 state 数据
    state: () => ({
      // 购物车的数组，用来存储购物车中每个商品的信息对象
      // 每个商品的信息对象，都包含如下 6 个属性：
      // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state_checked }
      // 从本地获取购物车数据,没有则给一个空字符串
      cart: JSON.parse(uni.getStorageSync('cart') || '[]')
    }),
    
    // 方法
    mutations:{
      // 添加至购物车
      addToCart(state,goods){
        // 使用find函数查询购物车中是否已有该物品，如果有则数量加一，无则添加物品
        const findResult = state.cart.find(x => x.goods_id === goods.goods_id && x.goods_sizes_detail_id == goods.goods_sizes_detail_id)
        // console.log(findResult)
        
        if(!findResult){
          state.cart.push(goods)
        }else{
          findResult.goods_count = goods.goods_count
        }
        
        // 调用saveToStorage方法
        this.commit('m_cart/saveToStorage')
      },
      
      // 储存购物车数据
      saveToStorage(state){
        uni.setStorageSync('cart',JSON.stringify(state.cart))
        
        // 给后端一份
        // const cartArray = state.cart
        
        // console.log(cart)
      },
      
      // 更新购物车商品勾选状态
      updateGoodsState(state,goods){
        const findResult = state.cart.find(x => x.goods_id === goods.goods_id)
        
        // 如果有该物品
        if(findResult){
          findResult.goods_state_checked = goods.goods_state_checked
          
          this.commit('m_cart/saveToStorage')
        }        
      },
      
      // 更新商品数量
      updateGoodsCount(state,goods){
        const findResult = state.cart.find(x => x.goods_id === goods.goods_id)
        
        if(findResult){
          findResult.goods_count = goods.goods_count
          this.commit('m_cart/saveToStorage')
        }
        
      },
      
      // 删除商品
      removeGoodsById(state,goods){
        state.cart = state.cart.filter(x => x.goods_id !== goods.goods_id || x.goods_sizes_detail_id !== goods.goods_sizes_detail_id)
        
        this.commit('m_cart/saveToStorage')
      },
      
      // 勾选或取消勾选全选时统一给商品赋值
      updateAllGoodsState(state,newState){
        state.cart.forEach(x => x.goods_state_checked = newState)
        
        this.commit('m_cart/saveToStorage')
      },
      

    },
    
    // 获取数值的方法
    getters:{
      
      // 统计购物车物品总数以获取购物车小红点数值
      total(state){
        // let b = 0
        // // 用forEach方法将每项的goods_count加起来
        // state.cart.forEach(x => b += x.goods_count)
        // return b
        
        return state.cart.reduce((total,item) =>{return total += item.goods_count},0)
      },
      
      // 统计购物车已勾选物品总数
      checkedCount(state){
        // filter函数过滤出已勾选商品的新数组，然后再用reduce()方法进行累加,0为初始值，循环每一项
        return state.cart.filter(x => x.goods_state_checked === true).reduce((total,item) => {return total += item.goods_count},0)
      },
      
      // 统计已勾选物品总价格
      totalPrice(state){
        return (state.cart.filter(x => x.goods_state_checked === true).reduce((total,item) => {return total += item.goods_price * item.goods_count},0)).toFixed(2)
      }
      
    }
    
    

}